#!/usr/bin/python
# File: processing_iobound.py
# Author: Miguel Sousa Filipe

from processing import Process, Queue
import time
import sys

work_counter = 0
worker_queue = [] 
workers = [] 
 
def setup_workers(ncpus, exec_fn):
    i = 0
    while  i < ncpus:
        worker_queue.append(Queue())
        workers.append( Process(target=exec_fn, args=[worker_queue[i]]) )
        workers[i].start()
        i += 1

def submit_work(*args)
	global work_counter
    worker_queue[ work_counter % len(worker_queue) ].put(args)
    work_counter += 1


def copy(filename, x):
    source = open(filename)
    contents = source.read()
    source.close()

    dest = open('temp/%d' % x, 'w')
    dest.write(contents)
    dest.close()


def worker_run(q):
    while 1:
        x = q.get()
        copy(filename,x)
        if(q.empty()): break


filename = ''

start_time = time.time()

inputs = xrange(0, 1000)


if len(sys.argv) > 1:
    ncpus = int(sys.argv[1])
else:
    ncpus = len(inputs)
print "Starting procesing with ",ncpus, "workers"

#create workers and queues for them
setup_workers(ncpus, worker_run)

#dispatch work 
for x in inputs:
    submit_work(x)

#wait for all to finish
for w in workers:
    w.join()


print "Time elapsed: ", time.time() - start_time, "s"
